package org.linlinjava.litemall.db.service;

 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.linlinjava.litemall.db.dao.LumierePointExchangeMapper;
import org.linlinjava.litemall.db.domain.LumierePointExchange;
import org.linlinjava.litemall.db.domain.LumierePointExchangeExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LumierePointExchangeService extends MPJBaseServiceImpl<LumierePointExchangeMapper,LumierePointExchange> {

    @Resource
    private LumierePointExchangeMapper lumierePointExchangeMapper;


    public Page<LumierePointExchange> selectPage(String pointExchangeName, Integer page, Integer size){
        LambdaQueryChainWrapper<LumierePointExchange> queryChainWrapper = new LambdaQueryChainWrapper<>(lumierePointExchangeMapper);
        return queryChainWrapper
                .like(StrUtil.isNotEmpty(pointExchangeName),LumierePointExchange::getName,pointExchangeName)
                .orderByDesc(LumierePointExchange::getAddTime)
                .page(new Page(page,size));
    }

    public List<LumierePointExchange> queryByName(String name){
        LambdaQueryChainWrapper<LumierePointExchange> queryChainWrapper = new LambdaQueryChainWrapper<>(lumierePointExchangeMapper);
        return queryChainWrapper
                .like(StrUtil.isNotEmpty(name),LumierePointExchange::getName,name)
                .orderByDesc(LumierePointExchange::getAddTime)
                .list();
    }


    /**
     * 添加积分兑换
     * @param lumierePointExchange
     * @return
     */
    public void add(LumierePointExchange lumierePointExchange){
        lumierePointExchangeMapper.insert(lumierePointExchange);
    }

    /**
     * 编辑积分兑换
     * @param lumierePointExchange
     * @return
     */
    public void update(LumierePointExchange lumierePointExchange){
        lumierePointExchangeMapper.updateById(lumierePointExchange);
    }

    public void delete(Integer pointExchangeId){
        lumierePointExchangeMapper.logicalDeleteByPrimaryKey(pointExchangeId);
    }

    public LumierePointExchange findById(Integer pointExchangeId){
        return lumierePointExchangeMapper.selectById(pointExchangeId);
    }

    public List<LumierePointExchange> list(String pointExchangeName,String sort,String order,Integer page,Integer size){
        LumierePointExchangeExample example = new LumierePointExchangeExample();

        if(pointExchangeName != null){
            example.or().andNameLike("%"+pointExchangeName+"%").andDeletedEqualTo(false);
        }else{
            example.or().andDeletedEqualTo(false);
        }

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }


        //PageHelper.startPage(page, size);

        return lumierePointExchangeMapper.selectByExample(example);
    }

    public List<LumierePointExchange> listCategory(){
        LumierePointExchangeExample example = new LumierePointExchangeExample();
        return lumierePointExchangeMapper.selectByExample(example);
    }

    public List<LumierePointExchange> listByCategory(Integer categoryId){
        LumierePointExchangeExample example = new LumierePointExchangeExample();
//        example.or().andCategoryIdEqualTo(categoryId);
        return lumierePointExchangeMapper.selectByExample(example);
    }



    public List<LumierePointExchange> sameLikeName(String name){
        LumierePointExchangeExample example = new LumierePointExchangeExample();
        example.or().andNameLike(name);
        return lumierePointExchangeMapper.selectByExample(example);
    }

    public List<LumierePointExchange> queryAll(){
        LambdaQueryChainWrapper<LumierePointExchange> queryChainWrapper = new LambdaQueryChainWrapper<>(lumierePointExchangeMapper);
        return queryChainWrapper
                .eq(LumierePointExchange::getStatus,1)
                .orderByDesc(LumierePointExchange::getAddTime)
                .list();
    }


}
